package com.chocho.producerserver.service;

import com.chocho.producerserver.bean.Question;
import com.chocho.producerserver.bean.Test;
import com.chocho.producerserver.client.ConsumeClient;
import com.chocho.producerserver.dao.QuestionDao;
import com.chocho.producerserver.dao.TestDao;
import com.chocho.producerserver.utils.UUIDUtil;
import com.chocho.producerserver.vo.Code;
import com.chocho.producerserver.vo.VQuestion;
import com.chocho.producerserver.vo.VTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chocho on 2022/4/12.
 */
@Service
public class TestService {

    @Autowired
    private TestDao testDao;
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private ConsumeClient consumeClient;

    public boolean addTest(HttpServletRequest request, VTest v){
        Test test = new Test();
        test.setTestId(UUIDUtil.getUUID());
        test.setTestName(v.getName());
        test.setTestScore(v.getScore());
        test.setTestNote(v.getNote());
        test.setTestOutput(v.getOutput());
        test.setQuestionId(v.getQuestionId());
        test.setTestInput(v.getInput());
        //刚存储到数据库，先默认状态为0
        test.setTestStatus(0);
        //1 将样例存储到数据库
        testDao.insert(test);

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("questionId", v.getQuestionId());
        List<Question> questionList = questionDao.getQuestionByQuery(map);
        if(questionList != null && questionList.size() > 0) {
            Question question = questionList.get(0);
            //2 从数据库获取标准答案
            String standardAnswer = question.getQuestionAnswerOne() +
                                    question.getQuestionAnswerTwo() +
                                    question.getQuestionAnswerThree();
            //3 生成测试程序
            String testCode = transTestCode(standardAnswer, v.getInput(), v.getOutput());

            //4 执行测试程序
            Code code = new Code();
            code.setSource(testCode);
            code.setQuestionId(question.getQuestionId());
            System.out.println("测试程序" + code.getSource());
            Map<String, Object> resultMap = consumeClient.JavaCompile(code);
            System.out.println("resultMap=========>" + resultMap.size());

            //5 评估测试结果
            String status = (String)resultMap.get("code");
            System.out.println("status=========>" + status);
            String res = (String)resultMap.get("res");
            System.out.println("res=========>" + res);

            if("200".equals(status) && v.getOutput().equals(res)){
                test.setTestStatus(1);
                testDao.update(test);
                return true;
            }
        }

        return false;
    }

    public String transTestCode(String standardAnswer, String input, String output){
        //输入 chocho_input
        String incode = standardAnswer.replaceAll("chocho_input", input);

        return incode;
    }

    public List<VTest> getTestByQuery(Map<String, Object> map){
        List<Test> list = testDao.getTestByQuery(map);
        List<VTest> testList = new ArrayList<>();
        for(Test t : list){
            VTest v = new VTest();
            v.setId(t.getTestId());
            v.setName(t.getTestName());
            v.setScore(t.getTestScore());
            v.setNote(t.getTestNote());
            v.setOutput(t.getTestOutput());
            v.setInput(t.getTestInput());
            v.setQuestionId(t.getQuestionId());
            v.setStatus(t.getTestStatus());

            testList.add(v);
        }

        return testList;
    }

    public boolean deleteTest(String ids){
        List<String> idList = new ArrayList<>();
        String []idArr = ids.split("&");
        for(int i = 0; i < idArr.length; i++){
            idList.add(idArr[i]);
        }

        return testDao.delete(idList) > 0;
    }

    public boolean updateTest(VTest v){
        Test test = new Test();
        test.setTestId(v.getId());
        test.setTestName(v.getName());
        test.setTestScore(v.getScore());
        test.setTestNote(v.getNote());
        test.setTestOutput(v.getOutput());
        test.setQuestionId(v.getQuestionId());
        test.setTestInput(v.getInput());
        test.setTestStatus(v.getStatus());

        return testDao.update(test) > 0;
    }

    public List<Test> getByIdList(List<String> ids){
        return testDao.getByIdList(ids);
    }
}
